package com.zn.asycn;

import com.zn.bo.WaterMarkBO;
import com.zn.entity.AppAgreement;
import com.zn.entity.AppCustomer;
import com.zn.entity.CustomerAgreement;
import com.zn.mapper.AppAgreementMapper;
import com.zn.mapper.AppCustomerMapper;
import com.zn.mapper.CustomerAgreementMapper;
import com.zn.util.DateUtils;
import com.zn.util.OSSUtil;
import com.zn.util.UUIDUtils;
import com.zn.util.WatermarkPic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 上传水印图
 * @author YinJiaWei
 * @version 2019/1/10 15:26
 */
@Component
public class UploadWatermark {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadWatermark.class);
    @Value("${oss.end.point}")
    private  String endPoint;
    @Value("${oss.access.key.id}")
    private  String ossAccessKeyId;
    @Value("${oss.access.key.secret}")
    private  String ossAccessKeySecret;
    @Value("${oss.bucket.name}")
    private  String bucketName;
    @Value("${oss.url}")
    private  String ossUrl;
    @Autowired
    private CustomerAgreementMapper customerAgreementMapper;
    @Autowired
    private AppCustomerMapper appCustomerMapper;
    @Autowired
    private AppAgreementMapper appAgreementMapper;

    /**
     * 客户已知晓并创建水印图
     * @param appAgreement
     * @param customerId
     * @param customerTradeAccount
     */
    @Async("executorPool")
    public void customerKnownAndCreateWatermark(AppAgreement appAgreement,String customerId,String customerTradeAccount){
        LOGGER.info("customerKnownAndCreateWatermark--->"+appAgreement.toString()+"|"+customerId+"|"+customerTradeAccount);
        OSSUtil ossUtil=OSSUtil.getInstance(endPoint,ossAccessKeyId,ossAccessKeySecret,bucketName,ossUrl);
        //上传目录 代理商code/Certification
        String imgFolder = "CustomerAgreement/"+ customerTradeAccount;
        String fileName=appAgreement.getAgreementCode()+"_"+customerTradeAccount+"_"+System.currentTimeMillis()+".png";
        String effectiveDate=DateUtils.convertDateToString(new Date());
        String content="生效时间:"+effectiveDate;
        WaterMarkBO waterMarkBO=new WaterMarkBO(appAgreement.getAgreementPic(),content,customerId,appAgreement.getAgreementTitle());
        WatermarkPic.createMark(waterMarkBO, ossUtil, fileName, imgFolder);
        /**上传成功 生成数据**/
        CustomerAgreement customerAgreement=new CustomerAgreement();
        customerAgreement.setCustomerAgreementId(UUIDUtils.getUuid());
        customerAgreement.setAgreementId(appAgreement.getAgreementId());
        customerAgreement.setCustomerId(customerId);
        customerAgreement.setEffectiveDate(effectiveDate);
        customerAgreement.setEffectiveStatus(0);
        String filePath=ossUtil.getOssUrl() + "/" + imgFolder + "/" +fileName ;
        customerAgreement.setCustomerAgreementPic(filePath);
        customerAgreementMapper.insertSelective(customerAgreement);
    }
    /**
     * 根据CRM推送过来的 agreementId 进行客户图片更改
     * @param agreementId
     */
    @Async("executorPool")
    public void updateCustomerWatermark(String agreementId){
        LOGGER.info("updateCustomerWatermark--->" + agreementId);
        CustomerAgreement customerAgreementWhere=new CustomerAgreement();
        customerAgreementWhere.setAgreementId(agreementId);
        List<CustomerAgreement> customerAgreementList=customerAgreementMapper.selectBySelective(customerAgreementWhere);
        OSSUtil ossUtil=OSSUtil.getInstance(endPoint, ossAccessKeyId, ossAccessKeySecret, bucketName, ossUrl);
        AppAgreement appAgreement=new AppAgreement();
        appAgreement.setAgreementId(agreementId);
        List<AppAgreement> appAgreements=appAgreementMapper.selectBySelective(appAgreement);
        if(appAgreements!=null && appAgreements.size()>0){
            appAgreement=appAgreements.get(0);
        }else{
            LOGGER.error("updateCustomerWatermark select agreementId is null------>"+agreementId);
            return;
        }
        for(CustomerAgreement customerAgreement:customerAgreementList){
            AppCustomer appCustomer=appCustomerMapper.selectByPrimaryKey(customerAgreement.getCustomerId());
            if(appCustomer==null){
                LOGGER.error("updateCustomerWatermark select appCustomer is null------>"+customerAgreement.getCustomerId());
                continue;
            }
            //重新上传新的图片上传目录 代理商code/Certification
            String imgFolder = "CustomerAgreement/"+ appCustomer.getCustomerTradeAccount();
            String fileName=appAgreement.getAgreementCode()+"_"+appCustomer.getCustomerTradeAccount()+"_"+System.currentTimeMillis()+".png";
            String content="生效时间:"+customerAgreement.getEffectiveDate();
            WaterMarkBO waterMarkBO=new WaterMarkBO(appAgreement.getAgreementPic(),content,appCustomer.getCustomerId(),appAgreement.getAgreementTitle());
            /**生成新的水印图**/
            WatermarkPic.createMark(waterMarkBO, ossUtil, fileName, imgFolder);
            String filePath=ossUtil.getOssUrl() + "/" + imgFolder + "/" +fileName ;
            customerAgreement.setCustomerAgreementPic(filePath);
            /**修改新的生效后的协议图**/
            customerAgreementMapper.updateByPrimaryKeySelective(customerAgreement);
        }


    }
}
